package cc.linker.steplink.crm.merchant.controller;


import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchanth5.pojo.TreatyPo;
import cc.linker.steplink.crm.merchanth5.service.TreatyService;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 平台协议
 * @author huhj
 */

@RestController
@RequestMapping("/merchant/treaty")
public class TreatyController {

    @Autowired
    private TreatyService treatyService;
    @Autowired
    private UserInfoUtil userInfoUtil;


    /**
     * 获取列表
     * @param session
     * @return
     */
    @GetMapping("/getlist")
    public List<TreatyPo> getTreatyList(HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        return treatyService.getList(orgId);
    }

    /**
     * 编辑
     * @param session
     * @param param
     * @throws Exception
     */
    @PostMapping("/edit")
    public void edit(HttpServletRequest request,@RequestBody Map<String, Object> param){
        if (StringUtils.isEmpty(param.get("treatyId")) || StringUtils.isEmpty(param.get("treatyContent"))) {
            throw GlobalException.build("参数错误");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        param.put("orgId", orgId);
        treatyService.edit(param);
    }


    /**
     * 获取协议详情
     * @param session
     * @param treatyName 协议名称
     * @return
     * @throws Exception
     */
    @GetMapping("/gettreatyinfo")
    public TreatyPo getTreatyInfo(HttpServletRequest request, String treatyName) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String, Object> param = new HashMap<>();
        param.put("treatyName", treatyName);
        param.put("orgId", userinfo.getOrgId());
        return treatyService.getTreatyInfo(param);
    }


    /**
     * 重置
     * @param session
     * @param param
     * @throws Exception
     */
    @PostMapping("/reset")
    public void reSet(HttpServletRequest request, @RequestBody Map<String, Object> param) {
        if (StringUtils.isEmpty(param.get("treatyId")) || StringUtils.isEmpty(param.get("treatyName"))) {
            throw GlobalException.build("参数错误");
        }
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        param.put("orgId", orgId);
        treatyService.reSet(param);
    }

}
